package com.lebang.configuration.redis;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * 闲玩刷新用的db
 * @author Eliohn on 2019/9/2.
 * @version 1.0
 */


@Configuration
@PropertySource(value = "classpath:/application.yml")
@Slf4j
public class RedisConfigSdk {
    @Value("${spring.redis2.host}")
    private String host;

    @Value("${spring.redis2.port}")
    private int port;

    @Value("${spring.redis2.timeout}")
    private int timeout;

    @Value("${spring.redis2.jedis.pool.max-idle}")
    private int maxIdle;

    @Value("${spring.redis2.jedis.pool.max-wait}")
    private long maxWaitMillis;

    @Value("${spring.redis2.jedis.pool.min-idle}")
    private int minIdle;

    @Value("${spring.redis2.jedis.pool.max-active}")
    private int maxTotal;

    @Value("${spring.redis2.password}")
    private String password;

    @Value("${spring.redis2.block-when-exhausted}")
    private boolean blockWhenExhausted;
    @Value("${spring.redis2.database}")
    private int dbIndex;

    @Bean("sdkRedis")
    public JedisPool redisPoolFactory() throws Exception {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        //最大连接数
        jedisPoolConfig.setMaxTotal(maxTotal);
        //业务需要的最小连接数
        jedisPoolConfig.setMinIdle(minIdle);

        jedisPoolConfig.setMaxIdle(maxIdle);

        jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
        // 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true
        jedisPoolConfig.setBlockWhenExhausted(blockWhenExhausted);
        //空闲资源的检测周期
        jedisPoolConfig.setTimeBetweenEvictionRunsMillis(30000);
        //资源池中资源最小空闲时间
        jedisPoolConfig.setMinEvictableIdleTimeMillis(80000);
        // 是否开启空闲资源监测
        jedisPoolConfig.setTestWhileIdle(true);
        // 是否启用pool的jmx管理功能, 默认true
        jedisPoolConfig.setJmxEnabled(false);

        return new JedisPool(jedisPoolConfig, host, port, timeout, password, dbIndex);
    }




}